/**
 * 亿牛集团
 * 本源代码由亿牛集团及其作者共同所有，未经版权持有者的事先书面授权，
 * 不得使用、复制、修改、合并、发布、分发和/或销售本源代码的副本。
 *
 * @copyright Copyright (c) 2017 yiniu.com all rights reserved.
 */

package com.rico.simplejdbc.jdbc06.web;

import com.alibaba.druid.support.json.JSONUtils;
import com.querydsl.core.QueryResults;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.Projections;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.querydsl.sql.SQLExpressions;
import com.rico.simplejdbc.jdbc06.configs.DbSdkConfig;
import com.rico.simplejdbc.jdbc06.sdk.dao.SdkGameDao;
import com.rico.simplejdbc.jdbc06.sdk.entities.QSdkGame;
import com.rico.simplejdbc.jdbc06.sdk.entities.QSdkGameType;
import com.rico.simplejdbc.jdbc06.sdk.entities.SdkGame;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * @author yaoyongfeng
 */
@Controller
@RequestMapping(path = "/game-t")
public class SdkGameController {
    @Autowired
    SdkGameDao sdkGameDao;
    @PersistenceContext(unitName = DbSdkConfig.PERSISTENCE_UNIT_NAME)
    private EntityManager em;

    @Autowired
    SessionFactory sessionFactorySdk;

    @Autowired
    JPAQueryFactory jpaQueryFactorySdk;


    @GetMapping(path = "/lists")
    @ResponseBody
    public Iterable<SdkGame> lists() {
        Predicate predicate = QSdkGame.sdkGame.id.eq(1L);
        Iterable<SdkGame> all1 = sdkGameDao.findAll(predicate);
        return all1;
    }

    /**
     * 使用JpaQueryFactory查询
     *
     * @return
     */
    @RequestMapping(path = "/query")
    @ResponseBody
    public String jpaQueryFactory() {
        sessionFactorySdk.createEntityManager();
        JPAQueryFactory queryFactory = new JPAQueryFactory(em);
        JPAQuery<Tuple> tupleJPAQuery = queryFactory
                .select(QSdkGame.sdkGame.id, QSdkGame.sdkGame.name)
                .from(QSdkGame.sdkGame)
                .leftJoin(QSdkGameType.sdkGameType).on(QSdkGameType.sdkGameType.id.eq(QSdkGame.sdkGame.type))
                .where(QSdkGame.sdkGame.id.gt(1))
                .where(QSdkGame.sdkGame.status.gt(0))
                .orderBy(QSdkGame.sdkGame.id.desc())
                .orderBy(QSdkGame.sdkGame.name.asc())
                .offset(3)
                .limit(1);
        Tuple tuple = tupleJPAQuery.fetchOne();
        return tuple.toString();
    }

    /**
     * 使用JpaQueryFactory查询
     *
     * @return
     */
//    @RequestMapping(path = "/query2")
//    @ResponseBody
//    public String predicateQuery() {
//        JPAQuery<Tuple> tupleJPAQuery = jpaQueryFactorySdk
//                .select(QSdkGame.sdkGame.id, QSdkGame.sdkGame.name)
//                .from(QSdkGame.sdkGame)
//                .leftJoin(QSdkGameType.sdkGameType).on(QSdkGameType.sdkGameType.id.eq(QSdkGame.sdkGame.type))
//                .where(QSdkGame.sdkGame.id.gt(1))
//                .where(QSdkGame.sdkGame.status.gt(0))
//                .orderBy(QSdkGame.sdkGame.id.desc())
//                .orderBy(QSdkGame.sdkGame.name.asc())
//                .offset(3)
//                .limit(1);
//        Tuple tuple = tupleJPAQuery.fetchOne();
//        JPAQuery<Tuple> tupleJPAQuery1 = jpaQueryFactorySdk.select(QSdkGameType.sdkGameType.id, QSdkGameType.sdkGameType.name)
//                .from(QSdkGameType.sdkGameType)
//                .where(QSdkGameType.sdkGameType.id.gt(3))
//                .limit(2);
//        tupleJPAQuery1.fetchOne();
//        return tuple.toString();
//    }


}
